package com.zzld.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzld.core.service.BaseServiceImpl;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.sys.entity.Org;
import com.zzld.modules.sys.mapper.OrgMapper;
import com.zzld.modules.sys.service.IOrgService;

@Service("orgService")
public class OrgServiceImpl  extends BaseServiceImpl<Org> implements IOrgService {

	@Autowired
	private OrgMapper orgMapper;

	/**
	 * 获取机构树
	 * 
	 * @return
	 */
	public List<TreeGrid> treegrid() {
		List<TreeGrid> listree = new ArrayList<TreeGrid>();
		List<Org> list = orgMapper.selectTreeGird();
		if (list != null && list.size() > 0) {
			for (Org o : list) {
				TreeGrid grd = new TreeGrid();
				BeanUtils.copyProperties(o, grd);
				grd.setText(o.getName());
				listree.add(grd);
			}
		}
		return listree;
	}

	/**
	 * 根据id查询一条数据
	 * 
	 * @param id
	 * @return
	 */
	public Org Selectbyid(String id) {
		Org g = new Org();
		try {

			if (id != "") {
				g = orgMapper.selectByPrimaryKey(id);
				return g;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 修改/删除操作
	 * 
	 * @param y
	 * @param i 0删除:1修改
	 * @return
	 */
	public int Updel(Org y, byte i) {
		try {
			if (y.getId() != "") {
				if (i == 0) {
					if (orgMapper.selectbycount(y.getId()) == 0) {
						return (Integer) orgMapper
								.deleteByPrimaryKey(y.getId());
					}
				} else if (i == 1) {
					return (Integer) orgMapper.updateByPrimaryKey(y);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 新增操作
	 * @param o
	 * @return
	 */
	public int AddOrg(Org o) {
		try {
			if (o != null && o.getId() != "") {
				return (Integer) orgMapper.insert(o);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return 0;
	}

}
